package com.jyw.business.obstacles.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import com.jyw.business.obstacles.domain.dto.web.OrderProcessRecordDto;
import com.jyw.business.obstacles.domain.vo.web.ProcessVO;
import com.jyw.common.utils.DateUtils;
import com.jyw.common.utils.SecurityUtils;
import com.jyw.common.utils.bean.BeanUtils;
import com.jyw.system.utils.ConversionUtil;
import org.springframework.stereotype.Service;
import com.jyw.business.obstacles.mapper.ObsOrderProcessMapper;
import com.jyw.business.obstacles.domain.ObsOrderProcess;
import com.jyw.business.obstacles.service.IObsOrderProcessService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;

/**
 * 报障-工单处理流程Service业务层处理
 * 
 * @author system
 * @date 2023-08-17
 */
@Service
public class ObsOrderProcessServiceImpl extends ServiceImpl<ObsOrderProcessMapper,ObsOrderProcess> implements IObsOrderProcessService
{
    @Resource
    private ObsOrderProcessMapper obsOrderProcessMapper;

    /**
     * 查询报障-工单处理流程
     * 
     * @param id 报障-工单处理流程主键
     * @return 报障-工单处理流程
     */
    @Override
    public ObsOrderProcess selectObsOrderProcessById(Long id)
    {
        return obsOrderProcessMapper.selectObsOrderProcessById(id);
    }

    /**
     * 查询报障-工单处理流程列表
     * 
     * @param obsOrderProcess 报障-工单处理流程
     * @return 报障-工单处理流程
     */
    @Override
    public List<ObsOrderProcess> selectObsOrderProcessList(ObsOrderProcess obsOrderProcess)
    {
        return obsOrderProcessMapper.selectObsOrderProcessList(obsOrderProcess);
    }

    /**
     * 新增报障-工单处理流程
     * 
     * @param obsOrderProcess 报障-工单处理流程
     * @return 结果
     */
    @Override
    public int insertObsOrderProcess(ObsOrderProcess obsOrderProcess)
    {
        obsOrderProcess.setCreateTime(DateUtils.getNowDate());
        return obsOrderProcessMapper.insertObsOrderProcess(obsOrderProcess);
    }

    /**
     * 修改报障-工单处理流程
     * 
     * @param obsOrderProcess 报障-工单处理流程
     * @return 结果
     */
    @Override
    public int updateObsOrderProcess(ObsOrderProcess obsOrderProcess)
    {
        obsOrderProcess.setUpdateTime(DateUtils.getNowDate());
        return obsOrderProcessMapper.updateObsOrderProcess(obsOrderProcess);
    }

    /**
     * 批量删除报障-工单处理流程
     * 
     * @param ids 需要删除的报障-工单处理流程主键
     * @return 结果
     */
    @Override
    public int deleteObsOrderProcessByIds(Long[] ids)
    {
        return obsOrderProcessMapper.deleteObsOrderProcessByIds(ids);
    }

    /**
     * 删除报障-工单处理流程信息
     * 
     * @param id 报障-工单处理流程主键
     * @return 结果
     */
    @Override
    public int deleteObsOrderProcessById(Long id)
    {
        return obsOrderProcessMapper.deleteObsOrderProcessById(id);
    }

    @Override
    public void record(OrderProcessRecordDto dto) {
        ObsOrderProcess process = new ObsOrderProcess();
        BeanUtils.copyProperties(dto,process);
        if(!ObjectUtils.isEmpty(SecurityUtils.getAuthentication())){
            process.setCreateUser(SecurityUtils.getUserId());
        }
        process.setCreateTime(DateUtils.getNowDate());
        save(process);
    }

    @Override
    public List<ProcessVO> list(String orderNum) {
        //根据工单号查询流程记录
        List<ProcessVO> list = lambdaQuery()
                .eq(ObsOrderProcess::getOrderNum, orderNum)
                .orderByDesc(ObsOrderProcess::getId).list().stream().map(ProcessVO::new).collect(Collectors.toList());
        return ConversionUtil.conversionList(list,ProcessVO.class);
    }
}
